package com.lemon.cloud.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.cloud.admin.service.SysDictItemService;
import com.lemon.cloud.admin.service.SysDictService;
import com.lemon.cloud.admin.entity.SysDict;
import com.lemon.cloud.admin.entity.SysDictItem;
import com.lemon.cloud.admin.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Autowired
    private SysDictItemService dictItemService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        SysDict dict = this.getById(id);
        if (dict!=null){
            List<SysDictItem> list = dictItemService.list(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getType, dict.getType()));
            if (CollectionUtil.isNotEmpty(list)){
                list.forEach(item->{
                    dictItemService.delSysDictItem(item);
                });
            }
        }
        return super.removeById(id);
    }
}
